package com.zz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zz.common.Log;
import com.zz.common.R;
import com.zz.dto.PreplanDto;
import com.zz.dto.PublicityDto;
import com.zz.entity.Activity;
import com.zz.entity.Preplan;
import com.zz.entity.Publicity;
import com.zz.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 社团负责人：活动草稿箱
 */
@RestController
@RequestMapping("/draft")
@Slf4j
@CrossOrigin
@Validated
public class DraftController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private PreplanService preplanService;

    @Autowired
    private PublicityService publicityService;

    /**
     *社团负责人:往活动草稿箱里增加活动
     * 字段：活动名称,活动时间,活动地点,预期人数
     * 朱彦燕,接口测试成功
     * */
    @RequiresRoles("leader")
    @PostMapping("/addActivity")
    @CacheEvict(value = "draftCache",allEntries = true)
    @Transactional
    @Log(title = "社团负责人：活动草稿箱：增加活动", dataLocationType = "body")
    public R<String> addActivity(@RequestBody @Validated(Insert.class) Activity activity, HttpServletRequest request){
        return activityService.addActivity(activity, request);
    }

    /**
     * 活动草稿箱：删除活动
     * 朱彦燕
     * @param activity
     * 测试成功
     * */
    @RequiresRoles("leader")
    @DeleteMapping("/deleteActivity")
    @CacheEvict(value = "draftCache",allEntries = true)
    @Log(title = "社团负责人：活动草稿箱：删除活动", dataLocationType = "body")
    public R<String> deleteActivity(@RequestBody @Validated(Delete.class) Activity activity){
        return activityService.deleteActivity(activity);
    }

    /**
     * 活动草稿箱：编辑活动
     * 朱彦燕
     * @param activity
     * 字段：活动名称,活动时间,活动地点,预期人数
     * 测试
     * */
    @RequiresRoles("leader")
    @PutMapping("/editActivity")
    @CacheEvict(value = "draftCache",allEntries = true)
    @Log(title = "社团负责人：活动草稿箱：编辑活动",dataLocationType = "body")
    public R<String> editActivity(@RequestBody @Validated(Update.class) Activity activity){
        return activityService.editActivity(activity);
    }

    /**
     * 社团草稿箱活动分页展示
     * 朱彦燕
     * @param page
     * @param pageSize
     * 测试成功
     * */
    @RequiresRoles("leader")
    @GetMapping("/activityList")
    //@Cacheable(value="draftCache",key = "'activityDraft'+#page+#pageSize+#activityName",sync = true)
    @Log(title = "社团负责人：社团草稿箱活动分页展示", dataLocationType = "link")
    public R<Page> activityList(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                                @RequestParam @NotNull(message = "每页最大数不能为空") int pageSize,
                                @Nullable @RequestParam String activityName, HttpServletRequest request){
        return activityService.activityList(page, pageSize, activityName, request);
    }

    /**
     * 申请举办活动
     * @param activity
     * 测试成功
     * */
    @RequiresRoles("leader")
    @PutMapping("/applyActivity")
    @Log(title = "社团负责人：申请举办活动", dataLocationType = "body")
    public R<String> applyActivity(@RequestBody @Validated(Delete.class) Activity activity){
        return activityService.applyActivity(activity);
    }


    @RequiresRoles("leader")
    @GetMapping("/infoPublicity")
    @Cacheable(value="publicityCache",key = "#activityId",sync = true)
    @Log(title = "社团负责人：展示宣传方案", dataLocationType = "link")
    public R<PublicityDto> infoPublicity(@RequestParam @NotNull(message = "活动编码不能为空") int activityId){
        return publicityService.info(activityId);
    }


    @RequiresRoles("leader")
    @PostMapping("/addPublicity")
    @CacheEvict(value = "publicityCache",allEntries = true)
    @Log(title = "社团负责人：增加宣传方案", dataLocationType = "body")
    public R<String> addPublicity(@RequestBody @Validated(Insert.class) Publicity publicity,
                                  HttpServletRequest request){
        return publicityService.add(publicity,request);
    }

    @RequiresRoles("leader")
    @DeleteMapping("/deletePublicity")
    @CacheEvict(value = "publicityCache",allEntries = true)
    @Log(title = "社团负责人：删除宣传方案", dataLocationType = "body")
    public R<String> deletePublicity(@RequestBody @Validated(Delete.class) Publicity publicity){
        return publicityService.delete(publicity);
    }


    @RequiresRoles("leader")
    @PutMapping("/updatePublicity")
    @CacheEvict(value = "publicityCache",allEntries = true)
    @Log(title = "社团负责人：修改宣传方案", dataLocationType = "body")
    public R<String> updatePublicity(@RequestBody @Validated(Insert.class) Publicity publicity){
        return publicityService.update(publicity);
    }


    @RequiresRoles("leader")
    @GetMapping("/infoPreplan")
    @Cacheable(value="preplanCache",key = "#activityId",sync = true)
    @Log(title = "社团负责人：展示安全预案", dataLocationType = "link")
    public R<PreplanDto> infoPreplan(@RequestParam @NotNull(message = "活动编码不能为空") int activityId){
        return preplanService.info(activityId);
    }


    @RequiresRoles("leader")
    @PostMapping("/addPreplan")
    @CacheEvict(value = "preplanCache",allEntries = true)
    @Log(title = "社团负责人：增加安全预案", dataLocationType = "body")
    public R<String> addPreplan(@RequestBody @Validated(Insert.class) Preplan preplan, HttpServletRequest request){
        return preplanService.add(preplan,request);
    }


    @RequiresRoles("leader")
    @DeleteMapping("/deletePreplan")
    @CacheEvict(value = "preplanCache",allEntries = true)
    @Log(title = "社团负责人：删除安全预案", dataLocationType = "body")
    public R<String> deletePreplan(@RequestBody @Validated(Delete.class) Preplan preplan){
        return preplanService.delete(preplan);
    }


    @RequiresRoles("leader")
    @PutMapping("/updatePreplan")
    @CacheEvict(value = "preplanCache",allEntries = true)
    @Log(title = "社团负责人：修改安全预案", dataLocationType = "body")
    public R<String> updatePreplan(@RequestBody @Validated(Insert.class) Preplan preplan){
        return preplanService.update(preplan);
    }

}
